using Core;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class SpriteSlicer : MonoBehaviour, ISlice
{
    public Sprite[] Sprites;

    private int index;
    private Image image;

    public void Awake()
    {
        image = GetComponent<Image>();
    }

    public void Switch(int i)
    {
        image ??= GetComponent<Image>();
        //log.err($"SpriteSlicer.Switch 111 i={i}");
        if (Sprites == null || image == null) return;
        //log.err($"SpriteSlicer.Switch 222 i={i}");

        if (i < 0 || i >= Sprites.Length)
        {
            return;
        }
        //log.err($"SpriteSlicer.Switch 333 i={i}");

        index = i;
        image.sprite = Sprites[i];
    }

    public void SwitchNext()
    {
        if (Sprites == null || image) return;

        Switch((index + 1) % Sprites.Length);
    }

    public void SwitchPrev()
    {
        if (Sprites == null || image) return;

        Switch((index - 1 + Sprites.Length) % Sprites.Length);
    }
}
